package http

import (
	"gitee.com/captials-team/ubdframe/src/domain/dto/reqdata"
	"gitee.com/captials-team/ubdframe/src/domain/dto/respdata"
	"gitee.com/captials-team/ubdframe/src/domain/interfaces"
	"gitee.com/captials-team/ubdframe/src/pkg/gin_http"
	"gitee.com/captials-team/ubdframe/src/pkg/i18n"
	v1log "gitee.com/captials-team/ubdframe/src/pkg/logs"
	"github.com/gin-gonic/gin"
	"go.uber.org/dig"
	"golang.org/x/text/language"
)

type BaseDataController struct {
	l         v1log.ILog
	regionSrv interfaces.ItfRegionDistrict

	*gin_http.I18nController
}

func NewBaseDataController(di *dig.Container, l v1log.ILog, regionSrv interfaces.ItfRegionDistrict) *BaseDataController {
	ctr := &BaseDataController{
		l:         l,
		regionSrv: regionSrv,
	}

	//i18n
	{
		parser := i18n.NewParser()
		//parser.Bundle().MustParseMessageFileBytes([]byte(I18nEn), language.English.String()+".toml")
		//parser.Bundle().MustParseMessageFileBytes([]byte(I18nZh), language.Chinese.String()+".toml")
		//for name, path := range conf.I18nFiles {
		//	parser.Load(path, name+".toml")
		//	l.Info("load i18n path %s: %s, %s", name, path, parser.Lang(name).ParseMsg("Hello"))
		//}
		parser.SetLanguage(language.Chinese)
		parser.CacheLanguages()
		ctr.I18nController = &gin_http.I18nController{
			Parser: i18n.NewParser(),
		}
	}

	return ctr
}

// Regions godoc
// @Summary      行政区域查询
// @Description  行政区域查询
// @Tags         baseData
// @Accept       json
// @Produce      json
// @Param        param    body     reqdata.QueryRegionReq  true  "参数"
// @success 0  {object} respdata.ResponseData{data=[]dto.RegionItem} "返回成功"
// @Router       /data/regions [post]
func (ctr *BaseDataController) Regions(ctx *gin.Context) {
	var query reqdata.QueryRegionReq
	ctx.ShouldBindJSON(&query)

	regions, err := ctr.regionSrv.Districts(query.Parent)
	if err != nil {
		ctr.Response(ctx, respdata.CError.MData(err.Error()))
		return
	}

	ctr.Response(ctx, respdata.CSuccess.MData(regions))
	return
}
